﻿Public Partial Class Loading_Unloading
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "LOADING/UNLOADING MANAGEMENT "
            LoadData("0", "0", "0", "0", "")
            Dim ds As DataTable = clsGet.Get_Shore("", "L")
            If (ds.Rows.Count > 0) Then
                With ddlShore
                    .ClearSelection()
                    .DataTextField = "Shore_Name"
                    .DataValueField = "Shore_Code"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--Shore ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
            ds = clsGet.Get_ProductGroupMaster("", "L")
            If (ds.Rows.Count > 0) Then
                With ddlProduct
                    .ClearSelection()
                    .DataTextField = "Group_name"
                    .DataValueField = "Group_no"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--Product ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
            ddlWharf.Items.Clear()
            ddlWharf.Items.Insert(0, "<--กรุณาเลือก-->")
            ddlWharf.Items(0).Value = "0"
            ds = clsGet.Get_WharfAll("L")
            If (ds.Rows.Count > 0) Then
                With ddlWharf
                    .Items.Clear()
                    .ClearSelection()
                    .DataTextField = "Wharf"
                    .DataValueField = "BookingWharf"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--Wharf ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If

            ddlLine.Items.Clear()
            ddlLine.Items.Insert(0, "<--Line ทั้งหมด-->")
            ddlLine.Items(0).Value = "0"
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnNewLoading.Visible = False
            End If
            If (Request.QueryString("P3") & "" <> "Y") Then
                gridLoading_Unloading.Columns(13).Visible = False
            End If
        End If
    End Sub
    Private Sub LoadData(ByVal Shore As String, ByVal Line As String, ByVal Product As String, ByVal Wharf As String, ByVal Facility As String)
        ViewState("Loading") = clsGet.Get_LoadingUnloading(Shore, Line, Product, Wharf, Facility)
        gridLoading_Unloading.PageIndex = 0
        gridLoading_Unloading.DataSource = ViewState("Loading")
        gridLoading_Unloading.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridLoading_Unloading.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridLoading_Unloading.PageIndex = ddlPage.SelectedIndex
        gridLoading_Unloading.DataSource = ViewState("Loading")
        gridLoading_Unloading.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridLoading_Unloading.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridLoading_Unloading.PageIndex = 0
            Case "prev"
                If (gridLoading_Unloading.PageIndex <> 0) Then
                    gridLoading_Unloading.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridLoading_Unloading.PageIndex <> gridLoading_Unloading.PageCount) Then
                    gridLoading_Unloading.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridLoading_Unloading.PageIndex = gridLoading_Unloading.PageCount
        End Select
        gridLoading_Unloading.DataSource = ViewState("Loading")
        gridLoading_Unloading.DataBind()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadData("0", "0", "0", "0", "")
    End Sub

    Private Sub gridLoading_Unloading_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridLoading_Unloading.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridLoading_Unloading.PageIndex <> 0) Then
            Index = Index Mod (gridLoading_Unloading.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Session("Loading") = gridLoading_Unloading.DataKeys(Index).Values("Load_Code")
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "cmddelete" Then
            Dim Loading_Code As String = gridLoading_Unloading.DataKeys(Index).Values("Load_Code")
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteLoading_Unloading(Loading_Code, conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadData("0", "0", "0", "0", "")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub btnNewLoading_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewLoading.Click
        Session("Loading") = Nothing
        ModalPopupExtender1.Show()
    End Sub

    Private Sub gridLoading_Unloading_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridLoading_Unloading.RowDataBound
        If (e.Row.RowType = DataControlRowType.Header) Then
            ViewState("Load_Code") = ""
        End If
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (gridLoading_Unloading.DataKeys(e.Row.RowIndex).Values("Load_Code") & "" = ViewState("Load_Code")) Then
                Dim imgEdit As ImageButton = CType(e.Row.FindControl("imgedit"), ImageButton)
                Dim imgDelete As ImageButton = CType(e.Row.FindControl("imgdelete"), ImageButton)
                e.Row.Cells(0).Text = ""
                e.Row.Cells(1).Text = ""
                e.Row.Cells(2).Text = ""
                imgEdit.Visible = False
                imgDelete.Visible = False
            End If
            If (gridLoading_Unloading.DataKeys(e.Row.RowIndex).Values("Plus_Tank") & "" <> "0") Then
                e.Row.Cells(8).Text = e.Row.Cells(8).Text & " + (" & gridLoading_Unloading.DataKeys(e.Row.RowIndex).Values("Plus_Tank") & "T)"
            End If
            ViewState("Load_Code") = gridLoading_Unloading.DataKeys(e.Row.RowIndex).Values("Load_Code")
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridLoading_Unloading.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridLoading_Unloading.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridLoading_Unloading.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridLoading_Unloading.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub ddlShore_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlShore.SelectedIndexChanged
        If (ddlShore.SelectedValue = "0") Then
            With ddlLine
                .Items.Clear()
                .Items.Insert(0, "<--Line ทั้งหมด-->")
                .Items(0).Value = "0"
            End With
        Else
            Dim ds As DataTable = clsGet.Get_Line(ddlShore.SelectedValue, "")
            If (ds.Rows.Count > 0) Then
                With ddlLine
                    .Items.Clear()
                    .ClearSelection()
                    .DataTextField = "Line_Name"
                    .DataValueField = "Line_Code"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--Line ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadData(ddlShore.SelectedValue, ddlLine.SelectedValue, ddlProduct.SelectedValue, ddlWharf.SelectedValue, txtFacility.Text)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class